<?php
$page_title = 'eTAT| Add Question';
$access_level = 1;
$script = 'survey';
require_once '../../controller.php';
require_once '../../classes/Survey.php';
require_once '../../classes/Question.php';

$q_survey = new Survey();
$q_question = new Question();
if(isset($_SESSION['logged_in'])) {
	$instructor_id = $_SESSION['logged_in'];
}

if(isset($_POST['add_new_question'])) {
	if(isset($_POST['required'])){
		$required='Y';
	}
	if(isset($_POST['comment_box'])){
		$comment_box='Y';
	}
	
	if($_POST['type']=='Multiple Select'){
		$type= 'Check Box';
	}elseif($_POST['type']=='Single Select'){
		$type= 'Radio Button';
	}elseif($_POST['type']=='Drop Down List'){
		$type='Drop Down';
	}else{
		$type = $_POST['type'];
	}
	$sid = $_POST['sid'];
	$q_id = $_POST['q_id']; 
	$data = array(
	              'survey_id'=>$_POST['sid'],
	              'type'=>addslashes($type),
	              'required'=>$required,
	              'question'=>addslashes($_POST['question_text']),
	              'create_section'=>'',
	              'order'=>addslashes($_POST['order']),
	              'comment_box'=>$comment_box,
	              'response1'=>addslashes($_POST['answer_choice_1']),
	              'response2'=>addslashes($_POST['answer_choice_2']),
	              'response3'=>addslashes($_POST['answer_choice_3']),
	              'response4'=>addslashes($_POST['answer_choice_4']),
	              'response5'=>addslashes($_POST['answer_choice_5']),
	              'response6'=>addslashes($_POST['answer_choice_6'])
	            );
	            
	if(!isset($_REQUEST['qid'])){
		$q_question->addQuestion($data);
		header("location: question.php?sid=$sid");
	}else{
		$id = $_REQUEST['qid'];
		$q_question->updateQuestion($id,$data);
		header("location: question.php?sid=$sid");
	}            
	
}

if(isset($_POST['delete_question'])){
	$id = $_POST['edit'];
	$q_question->deleteQuestion($id);

}

if(isset($_POST['cancel_question'])){
	$sid = $_POST['sid'];
	header("location: interface.php");
}

if(!isset ($_REQUEST['sid'])){
	$sql = "SELECT MAX(`id`)FROM `survey` WHERE `instructor_id` = '$instructor_id'";
	$sid = mysql_query($sql)or die(mysql_error());
	$result = mysql_fetch_row($sid);

	foreach($result as $key=>$value){
		$survey_id = $value;
	}
    $num =0;

}else{
	$survey_id = $_REQUEST['sid'];
	$questions = $q_survey->getQuestions($survey_id);
	if($questions){
	foreach ($questions as $i => $values) {
		$question_id[$i] = $values->id;
		$type[$i] = $values->type;
		$required[$i] = $values->required;
		$question_value[$i] = $values->question	;
		$create_section[$i] = $values->create_section;
		$order[$i] = $values->order;
		$comment_box[$i]=$values->comment_box;
		$response1[$i] = $values->response1;
		$response2[$i] = $values->response2;
		$response3[$i] = $values->response3;
		$response4[$i] = $values->response4;
		$response5[$i] = $values->response5;
		$response6[$i] = $values->response6;
	}
	$num = sizeof($type);
	}
	
}

if(isset($_POST['add_section'])){
	$section='Y';
	$order = $q_question->getLargestOrder($survey_id);
	$order = $order+1;
	$data = array(
	              'survey_id'=>$survey_id,
	              'type'=>'Section Text',
	              'required'=>'',
	              'question'=>addslashes($_POST['section_text']),
	              'create_section'=>$section,
	              'order'=>$order,
	              'comment_box'=>'',
	              'response1'=>'',
	              'response2'=>'',
	              'response3'=>'',
	              'response4'=>'',
	              'response5'=>'',
	              'response6'=>''
	            );
	  $q_question->addQuestion($data);
		header("location: question.php?sid=$survey_id");          
	
}
 
if(isset ($_POST['edit_question'])){
	$qid = $_POST['edit'];
	$question = $q_question->getQuestion($qid);
	$q_type = $question->type;
	 if($question->type!='Section Text'){
	$q_type = $question->type;
	$q_required = $question->required;
	$q_question_value = $question->question;
	$q_comment_box = $question->comment_box;
	$q_order = $question->order;
	$q_response1 = $question->response1;
	$q_response2 = $question->response2;
	$q_response3 = $question->response3;
	$q_response4 = $question->response4;
	$q_response5 = $question->response5;
	$q_response6 = $question->response6;
	 }if($question->type=='Section Type'){
	  
	 }
}
require_once '../../header.php';
?>
<br></br>
<h1 align="center">Add Questions to Survey</h1>
<?php $survey = $q_survey->getSurvey($survey_id);
       $survey_name= $survey['name'];
       echo "<h1 align='center'><u>Survey Name:</u> ".$survey_name."</h1>";?>
<br></br>       
<table align="center">
<tr>
<td>
<input type="button" name="create_section" value="create_section" onclick="popup('section-box')">
</td>
<td>
<a href="order.php?sid=<?php echo $survey_id;?>"><input type="button" name="change_order" value="Change Question Order"></input></a>
</td>
<td>
<a href="branching.php?sid=<?php echo $survey_id;?>"><input type='button' name='set_conditions' value='Set Conditions'></input></a>
</td>
<td>
<a href="preview.php?sid=<?php echo $survey_id;?>"><input type='button' name='preview' value='Preview Survey'></input></a>
</td>
<td>
<a href="finish.php?sid=<?php echo $survey_id;?>"><input type='button' name='Finish' value='Finish'></input></a>
</td>
</tr>
</table>

<p align="right"><a onclick="popup('help-box')"><font color="blue"><u>Help</u></font></a></p>
<br></br>
<div id="question-create">
<table width="300px">
	<form name="question" method="post" 
		action="<?php  if(isset($_POST['edit_question'])&& $q_type!='Section Text'){echo 'question.php?qid='.$qid;}else{echo 'question.php';}?> ">
	<tr>
		<th width="100px"> Question Type:</th>

		<td><select id='q-type' name="type" style="width:290px">
			<option onclick='javascript:hideAnswers();javascript:showRequired();javascript:hideCommentBox();'
<?php if(isset($_POST['edit_question']) && $q_type =='Text')echo 'selected="selected"';?>>Text</option>
			<option onclick='javascript:hideAnswers();javascript:showRequired();javascript:showCommentBox();'
<?php if(isset($_POST['edit_question']) && $q_type =='Yes/No')echo 'selected="selected"';?>>Yes/No</option>
			<option onclick='javascript:showAnswers();javascript:showRequired();javascript:showCommentBox();'
<?php if(isset($_POST['edit_question']) && $q_type =='Radio Button')echo 'selected="selected"';?>>Single Select</option>
			<option onclick='javascript:showAnswers();javascript:showRequired();javascript:showCommentBox();'
<?php if(isset($_POST['edit_question']) && $q_type =='Check Box')echo 'selected="selected"';?>>Multiple Select
			</option>
			<option onclick='javascript:showAnswers();javascript:showRequired();javascript:showCommentBox();'
<?php if(isset($_POST['edit_question']) && $q_type =='Rate')echo 'selected="selected"';?>>Rate</option>
            <option onclick='javascript:hideAnswers();javascript:showRequired();javascript:showCommentBox();'
<?php if(isset($_POST['edit_question']) && $q_type =='Likert Scale'){echo 'selected="selected"';}?>>Likert
			Scale</option>
			<option onclick='javascript:showAnswers();javascript:showRequired();javascript:showCommentBox();'
<?php if(isset($_POST['edit_question']) && $q_type =='Drop Down')echo 'selected="selected"';?>>Drop
			Down List</option>
			<option onclick='javascript:hideAnswers();javascript:showRequired();javascript:showCommentBox();'
<?php if(isset($_POST['edit_question']) && $q_type =='Date')echo 'selected="selected"';?>>Date</option>
			<option onclick='javascript:hideAnswers();javascript:showRequired();javascript:showCommentBox();'
<?php if(isset($_POST['edit_question']) && $q_type =='Numeric'){echo 'selected="selected"';}?>>Numeric</option>
			<option onclick='javascript:hideAnswers();javascript:hideOptions();javascript:showCommentBox();'
<?php if(isset($_POST['edit_question']) && $q_type =='Descriptive Text'){echo 'selected="selected"';}?>>Descriptive
			Text</option>

		</select></td>

	</tr>

	<tr>
		<td>&nbsp;&nbsp;&nbsp;</td>
	</tr>
	<tr>
		<th> Question Text:</th>
		<td><textarea rows="5" cols="30" name="question_text"><?php if(isset($_POST['edit_question'])){echo $q_question_value;}?></textarea></td>
	</tr>
	<tr>
		<td>&nbsp;&nbsp;&nbsp;</td>
	</tr>
	<tr id ='required'>
		<th>Response Required:</th>
		<td><input type="checkbox" name="required" <?php if(isset($_POST['edit_question'])&& $q_required=='Y'){
			echo 'checked="checked"';
		}?>></td>
	</tr>
	<tr>
		<td>&nbsp;&nbsp;&nbsp;</td>
	</tr>
	<tr id ='create_comment_box'>
		<th>Attach Optional Comment Box:</th>
		<td><input type="checkbox" name="comment_box" <?php if(isset($_POST['edit_question'])&& $q_comment_box=='Y'){
			echo 'checked="checked"';
		}?>></td>
	</tr>
	<tr id='a1'>
		<td>&nbsp;&nbsp;&nbsp;</td>
	</tr>
	<tr id='a2'>
		<th>&nbsp;</th><td>Add answer options:</td>
	</tr>
	<tr>
		<td>&nbsp;&nbsp;&nbsp;</td>
	</tr>
	<tr id='a3'>
		<th>Answer 1:</th>
		<td><input type="text" name=" answer_choice_1"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response1;}?>"></td>
	</tr>
	<tr id='a4'>
		<th>Answer 2:</th>
		<td><input type="text" name=" answer_choice_2"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response2;}?>"></td>
	</tr>
	<tr id='a5'>
		<th>Answer 3:</th>
		<td><input type="text" name=" answer_choice_3"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response3;}?>"></td>
	</tr>
	<tr id='a6'>
		<th>Answer 4:</th>
		<td><input type="text" name=" answer_choice_4"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response4;}?>"></td>
	</tr>
	<tr id='a7'>
		<th>Answer 5:</th>
		<td><input type="text" name=" answer_choice_5"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response5;}?>"></td>
	</tr>
	<tr id='a8'>
		<th>Answer 6:</th>
		<td><input type="text" name=" answer_choice_6"
			value="<?php if(isset($_POST['edit_question'])){echo $q_response6;}?>"></td>
	</tr>
	<tr>
		<td>&nbsp;&nbsp;&nbsp;</td>
	</tr>
	<tr>
		<td><input type="hidden" name="sid" value=" <?php echo $survey_id;?>"></td></tr>
	<tr>
		<td><input type="hidden" name="q_id" value=" <?php if(isset($_POST['edit_question']) && $q_type !='Section Text'){echo $qid;}?>"></td></tr>	
	<tr>
		<td><input type="hidden" name="order" value=" <?php if(isset($_POST['edit_question']) && $q_type !='Section Text'){echo $q_order;}else { echo $q_question->getLargestOrder($survey_id)+1;}?>"></td></tr>	
		<tr>
		    <td><input type="submit" name="cancel_question" value="Cancel"></td>
		    
		    <td><input type="submit" name="add_new_question" value=" Save Question"></td>
			</tr>
	
	</form>
</table>


</div>

<div id="question-display">
<h1><strong>Existing Questions</strong></h1>
<form name="display" method="post" 	action="<?php echo 'question.php?sid='.$survey_id;?>">
<select id="edit_q" name="edit" style="width: 400px" size="8">

<?php 
    $question_num =1;
    for($i=0;$i<$num;$i++){
	if($create_section[$i]=='Y'){  ?>
	<option  disabled="disabled" value="<?php echo $question_id[$i]?>"><?php echo '<-------section break/ new section-------> ';?></option>

	<?php }
	if($type[$i]=='Section Text'){
		?>
	<option value="<?php echo $question_id[$i]?>"><?php echo '<strong>SECTION TEXT.</strong> '.$question_value[$i];?></option>
	;
	<?php }else{?>
	<option value="<?php echo $question_id[$i]?>"><?php echo $question_num.'. '.$question_value[$i];?></option>
	;
	<?php
	$question_num++;
	}
}
?>
</select> <br>
<br>
<input type="submit" name="delete_question" value="Delete">
<input name="edit_question" type="submit" value="Modify Question"> 

</form>
</div>

<?php 
print '<div>';
	  $form1 = new Form('section_form','post',true);
      $form1->textArea('section_text','Section Description',4,40);
      $form1->textInput('order','',array('type'=>'hidden','value'=>"$num+1"));
      $form1->button('add_section','Create Section',array('type'=>'submit','class'=>'right'),false);
	  $form1->button('section_cancel','Cancel',array('type'=>'button','class'=>'right','onclick'=>'close_popup();'),false);
	  $form1->text("<div class='clear'></div>");
	  print popup('section-box',$form1->build(),450,array('top'=>200,'right'=>300,'left'=>300));
      print '</div>';

      
print '<div>';
print popup('help-box','<p><b>Question type:</b> Select here the type of question you want to ask.</p><br>
            <p><b>Question Text:</b> enter here the text for question</p><br>
            <p><b>Response Required:</b> Check this box of answer to question is mandatory. student cannot proceed further without answering this question</p><br>
            <p><b>Optional Comment Box:</b> Check This Box if You want to attch a comment box with this question for geeting feedback from students regarding this particular question.</p><br>
            <p><b>Answer options:</b> Enter here all the possible options to the question for students to choose from. (If Applicable)</p><br>
            <p><b>Create Section:</b>Click here to create a new section in survey. Every section is displayed on new page and can be used to implement branching</p><br>
            <p><b>Existing Questions:</b> It lists all theexisting questions  according to their order. You can either modify or delete questions.</p>
            <input name= "close_help" type="button" onclick="close_popup();" value="Close">',450,array('top'=>248,'right'=>40,'left'=>500));

print '</div>';      




require_once '../../footer.php';
?>